package udp

import (
	"gitee.com/tsingeye/FreeEhome/config"
	"time"
)

//使用deviceID筛选出指定的UDPClient
func filterUDPClient(deviceID string) *udpClient {
	udpGateway.Lock()
	client, ok := udpGateway.UDPClients[deviceID]
	udpGateway.Unlock()

	if !ok {
		return nil
	}

	return client
}

//指定通道实时直播启动
func InviteStream(deviceID string, realChannelID int64, channelType string) (replyData map[string]interface{}) {
	client := filterUDPClient(deviceID)

	if client == nil {
		replyData = map[string]interface{}{
			"errCode": config.EHomeDeviceOffline,
			"errMsg":  config.HTTPCodeMap[config.EHomeDeviceOffline],
		}
		return
	}

	return client.requestInviteStream(realChannelID, channelType)
}

//Hook触发关闭实时直播流
func ByeStream(deviceID, session string) (replyData map[string]interface{}) {
	client := filterUDPClient(deviceID)

	if client == nil {
		replyData = map[string]interface{}{
			"errCode": config.EHomeDeviceOffline,
			"errMsg":  config.HTTPCodeMap[config.EHomeDeviceOffline],
		}
		return
	}

	return client.requestByeStream(session)
}

//查询指定通道录像文件
func QueryRecordedFiles(deviceID string, realChannelID, fileType int64, startTime, stopTime string) (replyData map[string]interface{}) {
	client := filterUDPClient(deviceID)

	if client == nil {
		replyData = map[string]interface{}{
			"errCode": config.EHomeDeviceOffline,
			"errMsg":  config.HTTPCodeMap[config.EHomeDeviceOffline],
		}
		return
	}

	replyData = client.requestQueryRecordedFiles(realChannelID, fileType, startTime, stopTime)

	if replyData["errCode"].(int64) == config.EHomeSuccessOK {
		replyData["recordedFiles"] = recursionLoop(client, realChannelID, fileType, stopTime, replyData["recordedFiles"].([]map[string]interface{}), make([]map[string]interface{}, 0))
	}

	return
}

func recursionLoop(client *udpClient, realChannelID, fileType int64, stopTime string, recordedFiles, replyRecordedFiles []map[string]interface{}) []map[string]interface{} {
	if len(recordedFiles) != 0 {
		replyRecordedFiles = append(replyRecordedFiles, recordedFiles...)
		startTime := recordedFiles[len(recordedFiles)-1:][0]["stopTime"].(string)

		if startTime < stopTime {
			timeData, _ := time.ParseInLocation(config.TimeLayout, startTime, time.Local)
			startTime = time.Unix(timeData.Unix()+1, 0).Format(config.TimeLayout)

			replyData := client.requestQueryRecordedFiles(realChannelID, fileType, startTime, stopTime)

			if replyData["errCode"].(int64) == config.EHomeSuccessOK {
				recordedFiles := replyData["recordedFiles"].([]map[string]interface{})
				return recursionLoop(client, realChannelID, fileType, stopTime, recordedFiles, replyRecordedFiles)
			}
		}
	}

	return replyRecordedFiles
}

//指定通道录像文件播放
func UploadFile(deviceID string, realChannelID int64, filename string) (replyData map[string]interface{}) {
	client := filterUDPClient(deviceID)

	if client == nil {
		replyData = map[string]interface{}{
			"errCode": config.EHomeDeviceOffline,
			"errMsg":  config.HTTPCodeMap[config.EHomeDeviceOffline],
		}
		return
	}

	return client.requestUploadFile(realChannelID, filename)
}

//云台控制
func PtzControl(deviceID string, realChannelID int64, ptzCmd , action string, speed int64) (replyData map[string]interface{}) {
	client := filterUDPClient(deviceID)

	if client == nil {
		replyData = map[string]interface{}{
			"errCode": config.EHomeDeviceOffline,
			"errMsg":  config.HTTPCodeMap[config.EHomeDeviceOffline],
		}
		return
	}
	replyData = client.ptzControl(realChannelID, ptzCmd, action, speed)

	return
}
